import { defineStore } from 'pinia'

interface PrintStatus {
  dialogFormVisible: boolean
  legendInfo: { name: string; picAsUrl: string }[]
}

export const usePrintStore = defineStore('print', {
  state: (): PrintStatus & PrintInfo => ({
    dialogFormVisible: false,
    layoutTemplate: 'A4_landscape',
    title: '德阳市地图',
    subtitle: '',
    copyright: '',
    author: '北京超图软件股份有限公司',
    additionMapScale: '',
    centerPoint: '',
    scaleText: '',
    style: 'BAR',
    segments: '',
    unit: 'METER',
    format: 'PDF',
    DPI: '96',
    mapScale: '',
    mapCenter: '',
    legendInfo: [],
    mapZoom: 0,
    mapBounds: { south: 0, west: 0, north: 0, east: 0 }
  }),
  getters: {
    showDialogForm: (state) => state.dialogFormVisible,
    printInfo: (state) => ({
      layoutTemplate: state.layoutTemplate,
      title: state.title,
      subtitle: state.subtitle,
      copyright: state.copyright,
      author: state.author,
      additionMapScale: state.additionMapScale,
      centerPoint: state.centerPoint,
      scaleText: state.scaleText,
      style: state.style,
      segments: state.segments,
      unit: state.unit,
      format: state.format,
      DPI: state.DPI,
      mapScale: state.mapScale,
      mapCenter: state.mapCenter,
      legendInfo: state.legendInfo,
      mapZoom: state.mapZoom,
      mapBounds: state.mapBounds
    }),
    basicInfo: (state) => ({
      layoutTemplate: state.layoutTemplate,
      title: state.title,
      subtitle: state.subtitle,
      copyright: state.copyright,
      author: state.author
    }),
    additionInfo: (state) => ({
      additionMapScale: state.additionMapScale,
      centerPoint: state.centerPoint
    }),
    scaleInfo: (state) => ({
      scaleText: state.scaleText,
      style: state.style,
      segments: state.segments,
      unit: state.unit
    }),
    outOptions: (state) => ({
      format: state.format,
      DPI: state.DPI,
      mapScale: state.mapScale,
      mapCenter: state.mapCenter
    })
  },
  actions: {
    setBool(value: boolean) {
      this.dialogFormVisible = value
    },
    setTrue() {
      this.setBool(true)
    },
    setFalse() {
      this.setBool(false)
    },
    toggle() {
      this.setBool(!this.dialogFormVisible)
    },
    submitBasicInfoForm(basicInfo: BasicInfo) {
      this.layoutTemplate = basicInfo.layoutTemplate
      this.title = basicInfo.title
      this.subtitle = basicInfo.subtitle || ''
      this.copyright = basicInfo.copyright || ''
      this.author = basicInfo.author
    },
    submitAdditionInfoForm(additionInfo: AdditionInfo) {
      this.additionMapScale = additionInfo.additionMapScale
      this.centerPoint = additionInfo.centerPoint
    },
    submitScaleInfoForm(scaleInfo: ScaleInfo) {
      this.scaleText = scaleInfo.scaleText || ''
      this.style = scaleInfo.style
      this.segments = scaleInfo.segments || ''
      this.unit = scaleInfo.unit
    },
    submitOutOptionsForm(outOptions: OutOptions) {
      this.format = outOptions.format
      this.DPI = outOptions.DPI
      this.mapScale = outOptions.mapScale
      this.mapCenter = outOptions.mapCenter
      this.mapZoom = outOptions.mapZoom || 0
      this.mapBounds = outOptions.mapBounds || { south: 0, west: 0, north: 0, east: 0 }
    },
    addLegendInfo(legendInfo: { name: string; picAsUrl: string }[]) {
      this.legendInfo = legendInfo
    },
    resetPrintInfo() {
      this.layoutTemplate = 'A4_landscape'
      this.title = '德阳市地图'
      this.subtitle = ''
      this.copyright = ''
      this.author = '北京超图软件股份有限公司'
      this.additionMapScale = ''
      this.centerPoint = ''
      this.scaleText = ''
      this.style = 'BAR'
      this.segments = ''
      this.unit = 'METER'
      this.format = 'PDF'
      this.DPI = '96'
      this.mapScale = ''
      this.mapCenter = ''
      this.mapZoom = 0
      this.mapBounds = { south: 0, west: 0, north: 0, east: 0 }
    }
  }
})
